package com.taotie.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.taotie.constant.MessageConstant;
import com.taotie.constant.StatusConstant;
import com.taotie.dto.SetmealDTO;
import com.taotie.dto.SetmealPageQueryDTO;
import com.taotie.entity.Dish;
import com.taotie.entity.Setmeal;
import com.taotie.entity.SetmealDish;
import com.taotie.exception.DeletionNotAllowedException;
import com.taotie.exception.SetmealEnableFailedException;
import com.taotie.mapper.DishMapper;
import com.taotie.mapper.SetmealDishMapper;
import com.taotie.mapper.SetmealMapper;
import com.taotie.result.PageResult;
import com.taotie.service.SetmealService;
import com.taotie.vo.DishItemVO;
import com.taotie.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmeamlServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public void saveWithFlavor(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //向套餐中插入数据
        setmealMapper.insert(setmeal);
        //获取生成套餐的id
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        //保存套餐和菜品的关系
        setmealDishMapper.insertBatch(setmealDishes);

    }


    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        int page = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();
        PageHelper.startPage(page, pageSize);
        Page<SetmealVO> pageResult = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(pageResult.getTotal(), pageResult.getResult());
    }

    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmealId = setmealMapper.getById(id);
            //套餐在起售中不能删除
            if (StatusConstant.ENABLE.equals(setmealId.getStatus())) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        ids.forEach(id -> {
            //删除套餐
            setmealMapper.deleteById(id);
            //删除套餐和菜品的相关关系
            setmealDishMapper.deleteBySetmealId(id);
        });

    }

    @Override
    public SetmealVO getByIdWithDish(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        Long setmealId = setmealDTO.getId();
        //删除套餐与菜品之间的关系
        setmealDishMapper.deleteBySetmealId(setmealId);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        //重新插入菜品与套餐之间的关系
        setmealDishMapper.insertBatch(setmealDishes);
    }


    @Override
    public void startOrStop(Integer status, Long id) {
        //套餐启售时先判断是否包含停售菜品
        if (StatusConstant.ENABLE.equals(status)) {
            //根据套餐id查询菜品信息
            List<Dish> dishIds = dishMapper.getBySetmealId(id);
            if (dishIds != null && !dishIds.isEmpty()) {
                //遍历菜品，如果有停售菜品便抛出异常
                for (Dish dishId : dishIds) {
                    if(StatusConstant.DISABLE.equals(dishId.getStatus())){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }
        //修改套餐状态(仅修改状态，并没有修改时间)
       setmealMapper.updateStatus(status,id);
        //构建Setmeal，用于填充修改时间
        Setmeal setmeal = new Setmeal();
        //根据id去修改，注解AutoFill可以自动填充时间
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
